<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Contact extends CI_Controller{

	public function index(){
		$data['content'] = CONTACTFORM; 
		$this->load->view('index', $data);
	}

	function send_form(){

		$mail_sent = false;
		//Laddar email helpern
		$this->load->helper('email');

		//ta ut värden från formulär
		if ($this->input->post('submit')) {
			
			$this->load->library('form_validation');

			//XSS filtrering, förhindrar script attacker.
			$this->form_validation->set_rules('name', 'your name', 'trim|required|xss_clean');
			$this->form_validation->set_rules('email', 'your email', 'required|xss_clean');
			$this->form_validation->set_rules('subject', 'subject', 'required|xss_clean');
			$this->form_validation->set_rules('message', 'message', 'required|xss_clean');
			$this->form_validation->set_error_delimiters('<ul class="ul-error"><li><em>','</em></li></ul>');

			//om validering är false, annars kör på
			if ($this->form_validation->run() == FALSE){
				$data['content'] = CONTACTFORM; 
				$this->load->view('index', $data);
			}
			else{
				$this->load->library('email');

				$name = $this->input->post('name');
				$contact_email = $this->input->post('email');
				$subject = $this->input->post('subject');
				$message = $this->input->post('message');
				$myemail = OWNER_MAIL_ADDRESS;

				$this->load->library('email');

				$this->email->from($contact_email, $name);
				$this->email->to($myemail);
				$this->email->subject($subject);
				$this->email->message($message);
				$this->email->send();

				$data['content'] = CONTACT_SENT_SUCCESS; 
				$this->load->view('index', $data);
				
			}
		}
	}
}